<template>
  <div :class="{ show: show }" class="header-search">
    <el-popover v-model="show" placement="bottom" width="400" trigger="click" v>
      <ul
        v-if="list.length"
        class="infinite-list"
        v-infinite-scroll="load"
        :infinite-scroll-disabled="loading"
        style="overflow: auto"
      >
        <li
          v-for="(item, index) in list"
          :key="item.id + index"
          @mouseenter.once="read(item.id)"
          :class="['infinite-list-item', item.readFlg === 1 ? '' : 'unread']"
        >
          <el-card shadow="hover" body-style="padding:8px 12px">
            <div>
              <div class="title">{{ item.title }}</div>
              <el-row :gutter="30">
                <el-col :span="18">
                  <div class="msg">{{ item.msg }}</div>
                  <div class="time">{{ item.createTime }}</div>
                </el-col>
                <el-col :span="6">
                  <div class="operation">
                    <el-button
                      v-if="item.type === 11"
                      size="small"
                      @click="goto('/pt_runner/runner')"
                      >去审核</el-button
                    >
                    <el-button
                      v-else-if="item.type === 12"
                      size="small"
                      @click="goto('/pt_order/wallet')"
                      >去审核</el-button
                    >
                  </div>
                </el-col>
              </el-row>
            </div>
          </el-card>
        </li>
      </ul>
      <div v-else>
        <div class="empty-message">当前没有新的消息</div>
      </div>
      <el-badge slot="reference" :value="total" class="item">
        <svg-icon class-name="search-icon" icon-class="message" />
      </el-badge>
    </el-popover>
  </div>
</template>

<script>
// fuse is a lightweight fuzzy-search module
// make search results more in line with expectations
import path from "path";
import request from "@/utils/request";
import slider from "./slider.vue";
export default {
  name: "HeaderSearch",
  components: {
    slider,
  },
  data() {
    return {
      list: [],
      loading: false,
      pageSize: 10,
      pageNum: 1,
      total: 0,
      show: false,
      fuse: undefined,
    };
  },
  computed: {
    routes() {
      return this.$store.getters.permission_routes;
    },
  },
  watch: {
    routes() {
      this.searchPool = this.generateRoutes(this.routes);
    },
    show(value) {
      if (value) {
        this.pageNum = 1;
        this.list = [];
        this.load();
      }
      // document.body.removeEventListener("click", this.close);
    },
  },
  mounted() {
    // this.load()
  },
  methods: {
    read(id) {
      request({
        url: `/sys/ws/read/${id}`,
        method: "put",
      }).then((res) => {
        this.list = this.list.map((d) => {
          if (d.id === id) {
            return {
              ...d,
              readFlg: 1,
            };
          } else {
            return d;
          }
        });
      });
    },
    delete(id) {
      request({
        url: `/sys/ws/del/${id}`,
        method: "put",
      }).then((res) => {
        this.list = this.list
          .map((d) => {
            if (d.id === id) {
              return null;
            } else {
              return d;
            }
          })
          .filter(Boolean);
      });
    },
    goto(link) {
      this.$router.push(link);
      this.show = false;
    },
    load() {
      this.loading = true;
      request({
        url: "/sys/ws/page/my",
        method: "get",
        query: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
        },
      }).then((res) => {
        this.total = res.data.total;
        if (this.pageNum < res.data.totalPage) {
          this.pageNum += 1;
        } else if (this.pageNum === res.data.totalPage) {
          if (this.list.length < res.data.total) {
            this.list = this.list.concat(res.data.list);
          }
        } else {
        }
        this.loading = false;
      });
    },
    // Filter out the routes that can be displayed in the sidebar
    // And generate the internationalized title
    generateRoutes(routes, basePath = "/", prefixTitle = []) {
      let res = [];

      for (const router of routes) {
        // skip hidden router
        if (router.hidden) {
          continue;
        }

        const data = {
          path: path.resolve(basePath, router.path),
          title: [...prefixTitle],
        };

        if (router.meta && router.meta.title) {
          data.title = [...data.title, router.meta.title];

          if (router.redirect !== "noRedirect") {
            // only push the routes with title
            // special case: need to exclude parent router without redirect
            res.push(data);
          }
        }

        // recursive child routes
        if (router.children) {
          const tempRoutes = this.generateRoutes(
            router.children,
            data.path,
            data.title
          );
          if (tempRoutes.length >= 1) {
            res = [...res, ...tempRoutes];
          }
        }
      }
      return res;
    },
  },
};
</script>

<style lang="scss" scoped>
.empty-message {
  height: 200px;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 16px;
  // line-height: 200px;
  color: #666;
}
.infinite-list {
  list-style: none;
  margin: 0;
  padding: 0;
  height: 400px;
  overflow-y: auto;
  &-item {
    margin-bottom: 20px;
    position: relative;
    &.unread::after {
      content: " ";
      position: absolute;
      right: 8px;
      top: 8px;
      width: 8px;
      height: 8px;
      border-radius: 50%;
      background: #ee3b32;
      position: absolute;
    }
    .title {
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #282828;
      line-height: 18px;
    }
    .msg {
      margin-top: 8px;
      font-size: 12px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #4b4b4b;
      line-height: 18px;

      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2; //多行在这里修改数字即可
      overflow: hidden;
      /* autoprefixer: ignore next */
      -webkit-box-orient: vertical;
    }
    .time {
      margin-top: 6px;
      font-size: 12px;
      font-weight: 400;
      color: #999;
    }
    .operation {
      margin-top: 10px;
    }
  }
}
.header-search {
  font-size: 0 !important;

  .search-icon {
    cursor: pointer;
    font-size: 18px;
    vertical-align: middle;
  }

  .header-search-select {
    font-size: 18px;
    transition: width 0.2s;
    width: 0;
    overflow: hidden;
    background: transparent;
    border-radius: 0;
    display: inline-block;
    vertical-align: middle;

    /deep/ .el-input__inner {
      border-radius: 0;
      border: 0;
      padding-left: 0;
      padding-right: 0;
      box-shadow: none !important;
      border-bottom: 1px solid #d9d9d9;
      vertical-align: middle;
    }
  }

  &.show {
    .header-search-select {
      width: 210px;
      margin-left: 10px;
    }
  }
}
</style>
